home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / program / slix0987.zip / READBMP.ZIP / BMP.BAS
BASIC Source File  |  1996-05-17  |  9KB  |  281 lines

  1. CLS
  2. INPUT "Filename to load: ", filename$
  3. OPEN filename$ FOR BINARY AS #1
  4.  
  5. header$ = SPACE$(14)
  6. sizing$ = SPACE$(4)
  7. GET #1, 1, header$
  8. GET #1, 15, sizing$
  9. bmpinfosize = CVI(sizing$)
  10. 'bmpinfosize - Is the size of the information header for the bitmap.
  11. '              Different bitmap versions have variations in filetypes.
  12. '              40 is a standard windows 3.1 bitmap.
  13. '              12 is for OS/2 bitmaps
  14. 'The next routine reads in the appropriate headers and colour tables.
  15. 'nbits is the number of bits per pixel - i.e. number of colours
  16. '1 bit = 2 colours, 4 bits = 16 colours, 8 bits = 256 colours, etc.
  17. 'the 24 bit mode does not have a palette, its colours are expressed as
  18. 'image data
  19.  
  20. 'Design of a windows 3.1 bitmap - Taken from bmp.zip on the
  21. 'x2ftp.oulu.fi ftp site under /pub/msdos/programming/formats
  22. 'Specifications for a Windows 3.1 bitmap. (.BMP)
  23. 'Email any questions/responses to me at zabudsk@ecf.utoronto.ca
  24. 'or post to alt.lang.basic or comp.lang.basic.misc.
  25.  
  26. '       | # of   |
  27. 'Offset | bytes  | Function (value)
  28. '-------+--------+--- General Picture information starts here---------
  29. '  0    |   2    | (BM) - Tells us that the picture is in bmp format
  30. '  2    |   4    | Size of the file (without header?)
  31. '  6    |   2    | (0) Reserved1 - Must be zero
  32. '  8    |   2    | (0) Reserved2 - Must be zero
  33. '  10   |   4    | Number of bytes offset of the picture data
  34. '-------+--------+--- Information Header starts here -----------------
  35. '  14   |   4    | (40/12) Size of information header (Win3.1/OS2)
  36. '  18   |   4    | Picture width in pixels
  37. '  22   |   4    | Picture Height in pixels
  38. '  26   |   2    | (1) Number of planes, must be 1
  39. '  28   |   2    | Number of bits per pixel (bpp), must be 1,4,8 or 24
  40. '  30   |   4    | (0) Compression - 0 means no compression, 1,2 are RLEs
  41. '  34   |   4    | Image size in bytes
  42. '  38   |   4    | picture width in pels per metre
  43. '  42   |   4    | picture height in pels per metre
  44. '  46   |   4    | (0) Number of colours used in the picture, 0 means all
  45. '  50   |   4    | (0) Number of important colours, 0 means all
  46. '-------+--------+--- Palette data starts here -----------------------
  47. '  54   |   1    | (b) - blue intensity component, color 0 - range 0 to 255
  48. '  55   |   1    | (g) - green intensity component, color 0 - range 0 to 255
  49. '  56   |   1    | (r) - red intensity component, color 0 - range 0 to 255
  50. '  57   |   1    | (0) - unused
  51. '  58   |   1    | (b) - blue intensity component, color 0 - range 0 to 255
  52. '  ...  | ...    |
  53. '  54   | 4*2^bpp| total range of palette
  54. '-------+--------+--- Image data starts here -------------------------
  55. '54+    | width* | Bitmap data starting at lower left portion of the
  56. '(4*2^n)| height*| image moving from left towards right. Moving up 1
  57. '       | (8/bpp)| pixel when at the right hand side of the image, starting
  58. '       |        | from the left side again, until the top right of the
  59. '       |        | image is reached
  60.  
  61. 'Note that this format is slightly different for a OS/2 Bitmap.
  62. 'The header is the same up to (but not including) bit 30-
  63. 'The palette colour values follow at bit 30, with the form...
  64. '1 byte blue intensity
  65. '1 byte green intensity
  66. '1 byte red intensity
  67. 'For each colour of the picture.
  68. 'Bitmapped image data follows the colour tables
  69.  
  70.  
  71. 'Special note: When storing 1 bit (2 colour) pictures.
  72. '8 horizontal pixels are packed into 1 byte. Each bit determines
  73. 'the colour of one pixel (colour 0 or colour 1)
  74.  
  75. '4 bit pictures (16 colours) use 2 nibbles (4 bits) for each pixel
  76. 'thus there are 2 pixels for each byte of image data.
  77.  
  78. '8 bit pictures use 1 byte per pixel. Each byte of image data
  79. 'represents one of 256 colours.
  80.  
  81. '24 bit pictures express colour values by using 3 bytes and each has a
  82. 'value between 0 and 255. The first byte is for red, the second is for
  83. 'green and the third is for blue. Thus (256)^3 or 2^24 of 16777216 different
  84. 'colours.
  85.  
  86. IF bmpinfosize = 12 THEN
  87.    infoheader$ = SPACE$(12)
  88.    GET #1, 15, infoheader$
  89.    nbits = CVI(MID$(infoheader$, 15, 4))
  90.   
  91.    IF nbits = 1 THEN
  92.       palet$ = SPACE$(6)
  93.       GET #1, bmpinfosize + 15, palet$
  94.    ELSEIF nbits = 4 THEN
  95.       palet$ = SPACE$(48)
  96.       GET #1, bmpinfosize + 15, palet$
  97.    ELSEIF nbits = 8 THEN
  98.       palet$ = SPACE$(768)
  99.       GET #1, bmpinfosize + 15, palet$
  100.    END IF
  101. ELSEIF bmpinfosize = 40 THEN
  102.    infoheader$ = SPACE$(40)
  103.    GET #1, 15, infoheader$
  104.    nbits = CVI(MID$(infoheader$, 15, 4))
  105.    IF nbits = 1 THEN
  106.       palet$ = SPACE$(8)
  107.       GET #1, bmpinfosize + 15, palet$
  108.    ELSEIF nbits = 4 THEN
  109.       palet$ = SPACE$(64)
  110.       GET #1, bmpinfosize + 15, palet$
  111.    ELSEIF nbits = 8 THEN
  112.       palet$ = SPACE$(1024)
  113.       GET #1, bmpinfosize + 15, palet$
  114.    END IF
  115. END IF
  116.      
  117.  
  118. ft$ = MID$(header$, 1, 2)
  119. PRINT "Type of file (Should be BM): "; ft$
  120.  
  121. filesize = CVL(MID$(header$, 3, 4))
  122. PRINT "Size of file: "; filesize
  123.  
  124. r1 = CVI(MID$(header$, 7, 2))
  125. PRINT "Reserved 1: "; r1
  126.  
  127. r2 = CVI(MID$(header$, 9, 2))
  128. PRINT "Reserved 2: "; r2
  129.  
  130. offset = CVL(MID$(header$, 11, 4))
  131. PRINT "Number of bytes offset from beginning: "; offset
  132.  
  133. PRINT
  134.  
  135. headersize = CVL(MID$(infoheader$, 1, 4))
  136. PRINT "Size of header: "; headersize
  137.  
  138. picwidth = CVL(MID$(infoheader$, 5, 4))
  139. PRINT "Width: "; picwidth
  140.  
  141. picheight = CVL(MID$(infoheader$, 9, 4))
  142. PRINT "Height: "; picheight
  143.  
  144. nplanes = CVI(MID$(infoheader$, 13, 4))
  145. PRINT "Planes: "; nplanes
  146.  
  147. PRINT "Bits per plane: "; nbits
  148.  
  149. PRINT
  150.  
  151. IF headersize = 40 THEN
  152.    PRINT "Compression: ";
  153.    comptype = CVL(MID$(infoheader$, 17, 4))
  154.    IF comptype = 0 THEN PRINT "None"
  155.    IF comptype = 1 THEN PRINT "Run Length - 8 Bits"
  156.    IF comptype = 2 THEN PRINT "Run Length - 4 Bits"
  157.   
  158.    imagesize = CVL(MID$(infoheader$, 21, 4))
  159.    PRINT "Image Size (bytes): "; imagesize
  160.   
  161.    xsize = CVL(MID$(infoheader$, 25, 4))
  162.    PRINT "X size (pixels per metre): "; xsize
  163.   
  164.    ysize = CVL(MID$(infoheader$, 29, 4))
  165.    PRINT "Y size (pixels per metre): "; ysize
  166.   
  167.    colorsused = CVL(MID$(infoheader$, 33, 4))
  168.    PRINT "Number of colours used: "; colorsused
  169.   
  170.    neededcolours = CVL(MID$(infoheader$, 37, 4))
  171.    PRINT "Number of important colours: "; neededcolours
  172. END IF
  173. PRINT
  174. PRINT "Press Any key to continue."
  175. WHILE INKEY$ = ""
  176. WEND
  177.  
  178. IF nbits = 1 THEN
  179.    SCREEN 11
  180. ELSEIF nbits = 4 THEN
  181.    SCREEN 12
  182. ELSEIF nbits = 8 OR nbits = 24 THEN
  183.    SCREEN 13
  184. END IF
  185. IF bmpinfosize = 40 THEN ngroups = 4
  186. IF bmpinfosize = 12 THEN ngroups = 3
  187.  
  188. IF nbits = 24 THEN
  189.    IF ngroups = 3 THEN
  190.       FOR c = 0 TO 63
  191.          d = c * 4
  192.          palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d)
  193.          palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d + 1)
  194.          palet$ = palet$ + CHR$(d) + CHR$(d + 1) + CHR$(d)
  195.          palet$ = palet$ + CHR$(d + 1) + CHR$(d) + CHR$(d)
  196.       NEXT c
  197.    ELSEIF ngroups = 4 THEN
  198.       FOR c = 0 TO 63
  199.          d = c * 4
  200.          palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d) + CHR$(0)
  201.          palet$ = palet$ + CHR$(d) + CHR$(d) + CHR$(d + 1) + CHR$(0)
  202.          palet$ = palet$ + CHR$(d) + CHR$(d + 1) + CHR$(d) + CHR$(0)
  203.          palet$ = palet$ + CHR$(d + 1) + CHR$(d) + CHR$(d) + CHR$(0)
  204.       NEXT c
  205.    END IF
  206. END IF
  207.  
  208. FOR x = 1 TO LEN(palet$) STEP ngroups
  209.    zb# = INT((ASC(MID$(palet$, x, 1))) / 4)
  210.    zg# = INT((ASC(MID$(palet$, x + 1, 1))) / 4)
  211.    zr# = INT((ASC(MID$(palet$, x + 2, 1))) / 4)
  212.    zc# = zb# * 65536# + zg# * 256# + zr#
  213.    cres = ASC(MID$(palet$, x + 3, 1))
  214.    PALETTE ((x - 1) / ngroups), zc#
  215. NEXT x
  216.  
  217. IF nbits = 24 THEN
  218.    y = picheight - 1
  219.    x = 0
  220.    dat$ = "   "
  221.    WHILE y >= 0
  222.       WHILE x < picwidth
  223.          GET 1, , dat$
  224.          p1 = INT((ASC(MID$(dat$, 1, 1)) + ASC(MID$(dat$, 1, 1)) + ASC(MID$(dat$, 1, 1))) / 3)
  225.          PSET (x, y), p1
  226.          x = x + 1
  227.       WEND
  228.       y = y - 1
  229.       x = 0
  230.    WEND
  231. ELSEIF nbits = 8 THEN
  232.    y = picheight - 1
  233.    x = 0
  234.    dat$ = " "
  235.    WHILE y >= 0
  236.       WHILE x < picwidth
  237.          GET 1, , dat$
  238.          PSET (x, y), ASC(dat$)
  239.          x = x + 1
  240.       WEND
  241.       y = y - 1
  242.       x = 0
  243.    WEND
  244. ELSEIF nbits = 4 THEN
  245.    y